import 'package:flutter/material.dart';
import 'package:cache/cache.dart';
import 'package:flutter_bilibili/http/dao/login_dao.dart';
import 'package:flutter_bilibili/navigator/hi_navigator.dart';
import 'package:flutter_bilibili/widget/appbar.dart';
import 'package:flutter_bilibili/util/login_button.dart';
import 'package:flutter_bilibili/util/string_util.dart';
import 'package:flutter_bilibili/util/toast.dart';
import 'package:flutter_bilibili/widget/login_effect.dart';
import 'package:flutter_bilibili/widget/login_input.dart';

import 'package:yeyu_net/core/hi_error.dart';

class LoginPage extends StatefulWidget {
  const LoginPage({Key key}) : super(key: key);
  @override
  _LoginPageState createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
  bool protect = false;
  bool loginEnable = false;
  String userName;
  String password;

  TextEditingController _userNameController = TextEditingController();
  TextEditingController _passwordController = TextEditingController();
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }

  autoGet() {
    if (Cache.getInstance().get('userName') != null &&
        Cache.getInstance().get('password') != null) {
      _userNameController.text = Cache.getInstance().get('userName');
      _passwordController.text = Cache.getInstance().get('password');
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: appBar('密码登录', '注册', () {
        HiNavigator.getInstance().onJumpTo(RouteStatus.registration);
      }),
      body: Container(
        child: ListView(
          children: [
            LoginEffect(protect: protect),
            LoginInput(
              '用户名',
              '请输入用户名',
              controller: _userNameController,
              onChanged: (text) {
                userName = text;
                checkInput();
              },
            ),
            LoginInput('密码', '请输入密码',
                lineStretch: true,
                obscureText: true,
                controller: _passwordController, onChanged: (text) {
              password = text;
              checkInput();
            }),
            Padding(
              padding: EdgeInsets.only(left: 20, right: 20, top: 20),
              child: LoginButton(
                '登录',
                enable: loginEnable,
                onPressed: send,
              ),
            )
          ],
        ),
      ),
    );
  }

  void checkInput() {
    bool enable;
    if (isNotEmpty(userName) && isNotEmpty(password)) {
      enable = true;
    } else {
      enable = false;
    }
    setState(() {
      loginEnable = enable;
    });
  }

  send() async {
    try {
      var result = await LoginDao.login(userName, password);
      print(result);
      if (result['code'] == 0) {
        Cache.getInstance().setString('userName', userName);
        Cache.getInstance().setString('password', password);
        print(Cache.getInstance().get('userName'));
        print('登录成功');
        showToast('登录成功');
        HiNavigator.getInstance().onJumpTo(RouteStatus.home);
      } else {
        print(result['msg']);
        showToast(result['msg']);
      }
      // print(login_result);
    } on NeedAuth catch (e) {
      print(e);
      showToast(e.message);
    } on HiNetError catch (e) {
      print(e);
      showToast(e.message);
    }
  }
}
